package com.demo.androidgame.finsternis;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.gl.Animation;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLGraphics;
import com.demo.androidgame.finsternis.Assets;
import com.demo.androidgame.finsternis.Platform;

public class WorldRenderer { // 출력

	static final float FRUSTUM_WIDTH = 15;
	static final float FRUSTUM_HEIGHT = 10;
	
	GLGraphics glGraphics;
	
	World world;
	
	Camera2D cam;
	SpriteBatcher batcher;

	public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher,
			World world) {
		this.glGraphics = glGraphics;
		this.world = world;
		this.cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		this.batcher = batcher;
	}

	public void render() { // 데이터 바꿔놓고 그리기 작업할 때..
		if (world.bob.position.x <= 7) {
			cam.position.x = 7;
		} else if (world.bob.position.x >= 53) {
			cam.position.x = 53;
		} else {
			cam.position.x = world.bob.position.x;
		}
		
		cam.setViewportAndMatrices(); // 카메라의 위치기 바뀌므로 투영 위치도..

		renderBackground(); // 배경 그리기

		renderObjects(); // 요소 그리기
	}
	
	public void renderBackground() {
		cam.position.x = FRUSTUM_WIDTH; // 15
		
		batcher.beginBatch(Assets.lastbackground);
		batcher.drawSprite(0, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg1);
		batcher.drawSprite(14.9f, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg2);
		batcher.drawSprite(29.8f, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg3);
		batcher.drawSprite(44.7f, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg4);
		batcher.drawSprite(59.6f, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg5);
		batcher.drawSprite(74.5f, cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.lastbg6);

		batcher.endBatch();
		
	}

	public void renderObjects() {
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.frontbackground);
		batcher.drawSprite(0 -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg1);
		batcher.drawSprite(14.9f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg2);
		batcher.drawSprite(29.8f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg3);
		batcher.drawSprite(44.7f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg4);
		batcher.drawSprite(59.6f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg5);
		batcher.drawSprite(74.5f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg6);
		batcher.drawSprite(89.4f -(world.bob.position.x / 2), cam.position.y, 
				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
				Assets.frontbg7);
		batcher.endBatch();

		batcher.beginBatch(Assets.run);
		renderBob();
		batcher.endBatch();
		
		batcher.beginBatch(Assets.gate);
		renderTomb();
		renderTeleportPlatform();
		renderDoor();
		batcher.endBatch();
		
		batcher.beginBatch(Assets.platformArray);
		renderPlatforms();
		renderDummyPlatform();
		
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
	}
	
	private void renderPlatforms() {
		int len = world.platforms.size();
        for(int i = 0; i < len; i=i+5) {
            Platform platform = world.platforms.get(i);
            TextureRegion keyFrame = Assets.platform1;
            
                                   
            batcher.drawSprite(platform.position.x, platform.position.y, 
                            	1, 1, keyFrame);
        }
        
        for(int i = 1; i < len; i=i+5) {
            Platform platform = world.platforms.get(i);
            TextureRegion keyFrame = Assets.platform2;
            
                                   
            batcher.drawSprite(platform.position.x, platform.position.y, 
                            	1, 1, keyFrame);
        }
        
        for(int i = 2; i < len; i=i+5) {
            Platform platform = world.platforms.get(i);
            TextureRegion keyFrame = Assets.platform2;
            
                                   
            batcher.drawSprite(platform.position.x, platform.position.y, 
                            	1, 1, keyFrame);
        }
        
        for(int i = 3; i < len; i=i+5) {
            Platform platform = world.platforms.get(i);
            TextureRegion keyFrame = Assets.platform2;
            
                                   
            batcher.drawSprite(platform.position.x, platform.position.y, 
                            	1, 1, keyFrame);
        }
        
        for(int i = 4; i < len; i=i+5) {
            Platform platform = world.platforms.get(i);
            TextureRegion keyFrame = Assets.platform2;
            
                                   
            batcher.drawSprite(platform.position.x, platform.position.y, 
                            	1, 1, keyFrame);
        }      
    }
	
	private void renderDummyPlatform() {
		int len = world.dummyplatforms.size();
		for(int i = 0; i < len; i=i+5) {
            DummyPlatform dummyPlatform = world.dummyplatforms.get(i);
            TextureRegion keyFrame = Assets.dummyplatform1;
            
                                   
            batcher.drawSprite(dummyPlatform.position.x, dummyPlatform.position.y, 
                            	1, 1, keyFrame);
        }
		for(int i = 1; i < len; i=i+5) {
            DummyPlatform dummyPlatform = world.dummyplatforms.get(i);
            TextureRegion keyFrame = Assets.dummyplatform2;
            
                                   
            batcher.drawSprite(dummyPlatform.position.x, dummyPlatform.position.y, 
                            	1, 1, keyFrame);
        }
		for(int i = 2; i < len; i=i+5) {
            DummyPlatform dummyPlatform = world.dummyplatforms.get(i);
            TextureRegion keyFrame = Assets.dummyplatform3;
            
                                   
            batcher.drawSprite(dummyPlatform.position.x, dummyPlatform.position.y, 
                            	1, 1, keyFrame);
        }
		for(int i = 3; i < len; i=i+5) {
            DummyPlatform dummyPlatform = world.dummyplatforms.get(i);
            TextureRegion keyFrame = Assets.dummyplatform4;
            
                                   
            batcher.drawSprite(dummyPlatform.position.x, dummyPlatform.position.y, 
                            	1, 1, keyFrame);
        }
		for(int i = 4; i < len; i=i+5) {
            DummyPlatform dummyPlatform = world.dummyplatforms.get(i);
            TextureRegion keyFrame = Assets.dummyplatform5;
            
                                   
            batcher.drawSprite(dummyPlatform.position.x, dummyPlatform.position.y, 
                            	1, 1, keyFrame);
        }
            
    }
	

	private void renderBob() {
		TextureRegion keyFrame = null;
		keyFrame = Assets.bobrun.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
		
		float side = world.bob.velocity.x < 0 ? 1 : -1;
		batcher.drawSprite(world.bob.position.x, world.bob.position.y,
				side * 1, 1, keyFrame);
		
	}
	
	private void renderTomb() {
		Tomb tomb = world.tomb;
		batcher.drawSprite(tomb.position.x, tomb.position.y,
				1, 1, Assets.tomb);
	}
	
	private void renderTeleportPlatform() {
		int len = world.teleports.size();
        for(int i = 0; i < len; i=i+2) {
            TeleportPlatform teleport = world.teleports.get(i);
            TextureRegion keyFrame = Assets.teleportIn;
                                  
            batcher.drawSprite(teleport.position.x, teleport.position.y, 
                            	1, 1, keyFrame);
        }
        for(int i = 1; i < len; i=i+2) {
            TeleportPlatform teleport = world.teleports.get(i);
            TextureRegion keyFrame = Assets.teleportOut;
                                  
            batcher.drawSprite(teleport.position.x, teleport.position.y, 
                            	1, 1, keyFrame);
        }
        
	}
	
	private void renderDoor() {
		Door door = world.door;
		batcher.drawSprite(door.position.x, door.position.y,
				1, 1, Assets.door);
	}
}
